from Tools.lunar.names import *


class GregorianCalendar():
    def __init__(self, year, month, day, hour=0, minute=0, second=0):
        self.year = year
        self.month = month
        self.day = day
        self.hour = hour
        self.minute = minute
        self.second = second

    def re(self):
        return "1"

    def is_gregorian_days(self):
        """判断是否是格林历法，这里按照罗马教皇的时间安排方式"""
        if self.year == 1582 and self.month == 10:
            if 5 > self.day >= 15:
                return False
            else:
                return True
        else:
            return True

    def is_leap_year(self):
        """判断是否是闰年，但是不包括大数字年份"""
        return ((self.year % 4 == 0) and (self.year % 100 != 0)) or (self.year % 400 == 0)

    def calculate_julian_day(self):
        """
        计算儒略历时间
        儒略历定义：公元前4713年1月1日 UTC 12:00开始所经过的天数，
        JD0就被指定为公元前4713年1月1日 12:00到公元前4713年1月 2日12:00之间的24小时
        """
        a = int((14 - self.month) / 12)
        y = self.year + 4800 - a
        m = self.month + 12 * a - 3
        jdn = self.day + int((153 * m + 2) / 5) + 365 * y + int(y / 4)
        if self.is_gregorian_days():
            jdn = jdn - int(y / 100) + int(y / 400) - 32045.5
        else:
            jdn -= 32083.5
        return jdn + self.hour / 24.0 + self.minute / 1440.0 + self.second / 86400.0

    @staticmethod
    def zeller_week(year, month, day):
        """
        利用蔡勒公式计算星期数
        """
        m = month
        d = day
        if month <= 2:  # 对小于的月份进行修正
            year -= 1
            m = month + 12
        y = year % 100
        c = int(year / 100)

        w = (y + y / 4 + c / 4 - 2 * c + (13 * (m + 1) / 5) + d - 1) % 7
        if w < 0:  # 修正计算结果是负数的情况
            w += 7
        return int(w)

    def get_days_of_month(self):
        """
        计算本月的日期数
        """
        if (self.month < 1) or (self.month > 12):
            return 0

        days = daysOfMonth[self.month - 1]
        if (self.month == 2) and self.is_leap_year():
            days += 1
        return days

    def print_month_calendar(self):
        """
        打印本月的月历
        Su	Mo	Tu	We	Th	Fr	Sa
        		1	2	14	15	16
        17	18	19	20	21	22	23
        24	25	26	27	28	29	30
	
		"""
        print("Su  Mo  Tu  We  Th  Fr  Sa")
        # weekDayShow = self.InsertRowSpace((self.ZellerWeek(self.year,self.month,1)+1)*4+1) + "1"
        monthDay = 31
        if self.is_leap_year() and self.month == 2:
            monthDay = 29
        elif not self.is_leap_year() and self.month == 2:
            monthDay = 28
        weekDayShow = ""
        for x in range(1, monthDay):
            if x == 1 and self.zeller_week(self.year, self.month, x) != 6:  # 月第一天 先做第一个值的位置
                week = self.zeller_week(self.year, self.month, 1)
                weekDayShow = " " * (week * 4) + "1   "
            elif x == 1 and self.zeller_week(self.year, self.month, x) == 6:  # 星期六
                week = self.zeller_week(self.year, self.month, 1)
                weekDayShow = " " * (week * 4) + "1\n"
            elif self.zeller_week(self.year, self.month, x) == 6:  # 星期六
                weekDayShow += str(x) + "\n"
            elif self.zeller_week(self.year, self.month, x) == 0:  # 星期天
                weekDayShow += (str(x) + (" " * (2 - len(str(x))))) + "  "
            else:  # 其他部分 保持一天 四个字符
                weekDayShow += (str(x) + (" " * (2 - len(str(x))))) + "  "
        print(weekDayShow)


if __name__ == '__main__':
    # gc = GregorianCalendar(2016,9,30,13,24,59.01)
    # gc2 = GregorianCalendar(1977,3,27,12,0,0)
    gc1 = GregorianCalendar(2015, 11, 30, 12, 0, 0)
    # gc1 = GregorianCalendar(2016, 9, 30, 12, 0, 0)
    # print(gc2.CalculateJulianDay())
    # print(gc1.ZellerWeek(2016, 9, 30))
    gc1.print_month_calendar()
